package com.yellowpage.ca.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.UserGroup;
import com.yellowpage.ca.service.UserGroupService;

public class UserGroupAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_USERS = "users";

	private Configuration configuration;

	private List<UserGroup> userGroupList = new ArrayList<UserGroup>();
	private List<Map> resultList = new ArrayList<Map>();

	private UserGroup userGroup;
	private UserGroupService userGroupService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_name;
	private String query_displayName;
	
	private String query_userName;
	private String[] groupUsers = new String[0];	
	private List<Map> userInGroupList = new ArrayList<Map>();
	private List<Map> userOutGroupList = new ArrayList<Map>();

	

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.userGroup = userGroupService.getUserGroup(Long
					.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("name", query_name);
		queryMap.put("displayName", query_displayName);

		resultList = userGroupService
				.queryUserGroupByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		userGroup.setCreateTime(new Date());
		userGroup.setCreateUser(loginUser.getUserId());	
		userGroup.setLastUpdateTime(new Date());
		userGroup.setLastUpdateUser(loginUser.getUserId());	
		userGroup.setStatus("1");

		userGroupService.saveUserGroup(userGroup);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (userGroup.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		if (userGroup.getDisplayName().length() == 0) {
			this.addActionError("** 显示名称 不能为空 !");
		}
		
		this.setValidatorResult("/ca/UserGroupAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		userGroup = userGroupService.getUserGroup(id);
		return RESULT_EDIT;
	}

	public String update() {
		userGroup.setLastUpdateTime(new Date());
		userGroup.setLastUpdateUser(loginUser.getUserId());	

		userGroupService.updateUserGroup(userGroup);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (userGroup.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		if (userGroup.getDisplayName().length() == 0) {
			this.addActionError("** 显示名称 不能为空 !");
		}
		this.setValidatorResult("/ca/UserGroupUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		this.userGroupService.delete(ids);

		return queryByPage();
	}
	
	
	public String toUsers() {
		Long userGroupId = new Long(selectedId);
		userGroup = userGroupService.getUserGroup(userGroupId);
		
		userInGroupList = userGroupService.getUserWithGroup(userGroupId, true);
		userOutGroupList = userGroupService.getUserWithGroup(userGroupId, false);
		
		return RESULT_USERS;
	}
	
	public String saveUserWithGroup() {
		Long userGroupId = new Long(selectedId);
		userGroup = userGroupService.getUserGroup(userGroupId);
		
		List<Long> ids = new ArrayList();
		for (String userId : groupUsers) {
			ids.add(Long.valueOf(userId));
		}
		
		userGroupService.saveGroupUsers(ids, userGroupId, loginUser.getUserId());
		
		userInGroupList = userGroupService.getUserWithGroup(userGroupId, true);
		userOutGroupList = userGroupService.getUserWithGroup(userGroupId, false);
		
		return RESULT_USERS;
	}
	
	

	public List<Map> getUserInGroupList() {
		return userInGroupList;
	}

	public void setUserInGroupList(List<Map> userInGroupList) {
		this.userInGroupList = userInGroupList;
	}

	public List<Map> getUserOutGroupList() {
		return userOutGroupList;
	}

	public void setUserOutGroupList(List<Map> userOutGroupList) {
		this.userOutGroupList = userOutGroupList;
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setUserGroup(UserGroup userGroup) {
		this.userGroup = userGroup;
	}

	public UserGroup getUserGroup() {
		return userGroup;
	}

	public void setUserGroupService(UserGroupService userGroupService) {
		this.userGroupService = userGroupService;
	}

	public List<UserGroup> getUserGroupList() {
		return userGroupList;
	}

	public void setUserGroupList(List<UserGroup> userGroupList) {
		this.userGroupList = userGroupList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}	

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_displayName() {
		return this.query_displayName;
	}

	public void setQuery_displayName(String displayName) {
		this.query_displayName = displayName;
	}	
	
	public String getQuery_userName() {
		return query_userName;
	}

	public void setQuery_userName(String queryUserName) {
		query_userName = queryUserName;
	}
	
	public String[] getGroupUsers() {
		return groupUsers;
	}

	public void setGroupUsers(String[] groupUsers) {
		this.groupUsers = groupUsers;
	}


}
